<?php
namespace app\web\home;


use app\web\model\Comment as myModel;
use app\web\model\Order;
use think\Db;
/**
 * 首页\公共接口
 */
class Comment extends Base 
{
    /**
     * 提交评论
     * [create description]
     * @return [type] [description]
     */
    public function create()
    {
        $post = input('post.');
        $this->validate($post,'Comment');
        $order = Order::where('id',$post['order_id'])->field('id,shop_id,project_id,technician_id')->find();
        $post['shop_id'] = $order->shop_id;
        $post['technician_id'] = $order->technician_id;
        $post['project_id'] = $order->project_id;
        $post['user_id'] = $this->uid;
        $post['is_content'] = empty($post['content']) ? 0 : 1;
        $post['is_images'] = empty($post['images']) ? 0 : 1;
        $post['is_star'] = $post['star'] > 3 ? 2 : ($post['star'] < 3 ? 0 : 1);
        Db::startTrans();
        $res = Order::completeOrder($post['order_id']);
        if(!$res) return show(400,'评论失败');
        $res = myModel::create($post);
        if($res->isEmpty()) return show(400,'评论失败');
        $order->is_comment = 1;
        //$order->status = 4;
        if(!$order->save()){
            Db::rollback();
            return show(400,'评论失败');
        }
        $res = myModel::technicianScore($post['technician_id']);
        if(!$res){
            Db::rollback();
            return show(400,'评论失败');
        }
        if(!myModel::shopPraiseRate($post['shop_id'])){
            Db::rollback();
            return show(400,'评论失败'); 
        }
        Db::commit();
        return show(200,'评论成功');
    }
    /**
     * 用户端店铺评论
     * [getShopComment description]
     * @param  [type]  $id     [description]
     * @param  integer $status [description]
     * @return [type]          [description]
     */
    public function getShopComment($id,$status = 0)
    {
        $where = ['a.shop_id'=>$id];
        switch($status){
            case '1':
                $where['a.is_content'] = 1;
                break;
            case '2':
                $where['a.is_images'] = 1;
                break;
            case '3':
                $where['a.is_star'] = 2;
                break;
            case '4':
                $where['a.is_star'] = 1;
                break;
            case '5':
                $where['a.is_star'] = 0;
                break;
        }
        $list = myModel::alias('a')
                        ->where($where)
                        ->where('a.status',1)
                        ->leftjoin('user b','b.id=a.user_id')
                        ->field('a.id,b.avatarUrl,a.order_id,b.nickName,a.content,a.images,a.star,a.create_time as create_time1')
                        ->order('id desc')
                        ->paginate();
        return show(200,'评论列表',$list);
    }
    public function getCommentNumber($t_id=0,$shop_id=0)
    {
        if($shop_id > 0){
            $map = ['shop_id'=>$shop_id,'status'=>1];
        }elseif($t_id > 0){
            $map = ['technician_id'=>$t_id,'status'=>1];
        }
        
        $arr['tot_number'] = myModel::where($map)->count();
        $arr['content_number'] = myModel::where($map)->where(['is_content'=>1])->count();
        $arr['images_number'] = myModel::where($map)->where(['is_images'=>1])->count();
        $arr['star5_number'] = myModel::where($map)->where(['is_star'=>2])->count();
        $arr['star3_number'] = myModel::where($map)->where(['is_star'=>1])->count();
        $arr['star0_number'] = myModel::where($map)->where(['is_star'=>0])->count();
        return show(200,'评论数量',$arr);
    }
    /**
     * 用户端技师评论
     * [getShopComment description]
     * @param  [type]  $id     [description]
     * @param  integer $status [description]
     * @return [type]          [description]
     */
    public function getTComment($id,$status = 0)
    {
        $where = ['a.technician_id'=>$id];
        switch($status){
            case '1':
                $where['a.is_content'] = 1;
                break;
            case '2':
                $where['a.is_images'] = 1;
                break;
            case '3':
                $where['a.is_star'] = 2;
                break;
            case '4':
                $where['a.is_star'] = 1;
                break;
            case '5':
                $where['a.is_star'] = 0;
                break;
        }
        $list = myModel::alias('a')
                        ->where($where)
                        ->where('a.status',1)
                        ->leftjoin('user b','b.id=a.user_id')
                        ->field('a.id,b.avatarUrl,a.order_id,b.nickName,a.content,a.images,a.star,a.create_time as create_time1')
                        ->order('id desc')
                        ->paginate();
        return show(200,'评论列表',$list);
    }
    /**
     * 用户端技师端商户端/获取评价列表
     * [getTechnicianComment description]
     * @param  [type]  $id     [description]
     * @param  integer $status [description]
     * @return [type]          [description]
     */
    public function getCommentList()
    {
        if($this->appkey == 1){
            $where = ['a.technician_id'=>$this->getCurrentId()];
        }elseif($this->appkey == 2){
            $where = ['a.shop_id'=>$this->getCurrentId()];
        }else{
            $where = ['a.user_id'=>$this->uid];
        }
        $list = myModel::alias('a')
                        ->where($where)->where('a.status',1)
                        ->leftjoin('user b','b.id=a.user_id')
                        ->field('a.id,b.avatarUrl,a.order_id,b.nickName,a.content,a.images,a.star,a.create_time as create_time1')
                        ->order('id desc')
                        ->paginate();
        return show(200,'评论列表',$list);
    }
}